﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using WcfGeebaServer.ContentManage;
using WcfGeebaServer.Users;
using WcfGeebaServer.Policies;

namespace TestGeeba
{
    /// <summary>
    /// This is a test class for GForum and is intended
    /// to contain all GForum Unit Tests
    /// </summary>
    [TestClass]
    public class GThreadTest
    {
        private GThread testedThread;
        private TestContext testContextInstance;
        private Member admin;

        public GThreadTest()
        {
            GForum forum = GForum.GetForum();
            admin = new Member("Admin", "admin", true);
            GSubForum sf = new GSubForum("Topic");
            testedThread = new GThread("Test Thread", "Some Content", admin);
            forum.init(admin, new GPolicy());
            forum.addSubForum("Topic", admin);
            testedThread = new GThread(sf.publish(testedThread), testedThread);
        }

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }


        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void bumpTest()
        {
            Assert.IsTrue(testedThread.ModifiedDate.Equals(testedThread.PublishDate));
            DateTime date1 = testedThread.ModifiedDate;
            for (int i = 0; i < 10000000; i++) ;
            testedThread.bump();
            DateTime date2 = testedThread.ModifiedDate;
            Assert.IsTrue(date2.CompareTo(date1) > 0);
            for (int i = 0; i < 10000000; i++) ;
            testedThread.bump();
            DateTime date3 = testedThread.ModifiedDate;
            Assert.IsTrue(date3.CompareTo(date2) > 0);
        }

        [TestMethod]
        public void addPostTest()
        {
            int count = 0;
            if (testedThread.getPosts() != null)
                count = testedThread.getPosts().Count;
            Member publisher = new Member("Publisher", "1234", true);
            GForum.GetForum().register("Publisher", "1234");
            Assert.IsTrue(testedThread.getPosts().Count == count + 0);
            int postID1 = testedThread.addPost(new GPost("Subject1", "Content", publisher, testedThread));
            Assert.IsTrue(testedThread.getPosts().Count == count + 1);
            int postID2 = testedThread.addPost(new GPost("Subject2", "Content", publisher, testedThread));
            Assert.IsTrue(testedThread.getPosts().Count == count + 2);
            Assert.IsTrue(postID1 != postID2);
        }

        [TestMethod]
        public void removePostTest()
        {
            Assert.IsTrue(testedThread.getPosts().Count == 0);
            Member m = new Member("Gili", "12345", true);
            GForum.GetForum().register("Gili", "12345");
            int postID1 = testedThread.addPost(new GPost("Subject1", "Content", m, testedThread));
            int postID2 = testedThread.addPost(new GPost("Subject2", "Content", m, testedThread));
            Assert.IsTrue(testedThread.getPosts().Count == 2);
            //testedThread.removePost(postID1);
            //Assert.IsTrue(testedThread.getPosts().Count == 1);
            //testedThread.removePost(postID1);
            //Assert.IsTrue(testedThread.getPosts().Count == 1);
            //testedThread.removePost(postID2);
            //Assert.IsTrue(testedThread.getPosts().Count == 0);
        }

        [TestMethod]
        public void removePostWithPostsTest()
        {
            Member m = new Member("Gili", "12345", true);
            GForum.GetForum().register("Gili", "12345");
            int postID1 = testedThread.addPost(new GPost("Subject1", "Content", m, testedThread));
            GPost post = testedThread.GetPost(postID1);
            post.addPost(new GPost("Subject2", "Content", m, testedThread));
            post.addPost(new GPost("Subject3", "Content12", m, testedThread));
            Assert.IsTrue(testedThread.removePost(postID1));
        }

        [TestMethod]
        public void editContentTest()
        {
            Member publisher1 = new Member("Publisher", "1234", true);
            Member publisher2 = new Member("Publisher", "1234", true);
            Assert.IsFalse(testedThread.editContent(publisher1, "new content"));
            Assert.IsFalse(testedThread.editContent(publisher2, "new content"));
            Assert.IsTrue(testedThread.editContent(admin, "new content"));
        }

        [TestMethod]
        public void getPostsTest()
        {
            int count = 0;
            if (testedThread.getPosts() != null)
                count = testedThread.getPosts().Count;
            Member publisher = new Member("Publisher", "1234", true);
            GForum.GetForum().register("Publisher", "1234");
            GPost post1 = new GPost("Subject1", "Content", publisher, testedThread);
            GPost post2 = new GPost("Subject2", "Content", publisher, testedThread);
            Assert.IsTrue(testedThread.getPosts().Count == count + 0);
            int postID1 = testedThread.addPost(post1);
            Assert.IsTrue(testedThread.getPosts().Count == count + 1);
            int postID2 = testedThread.addPost(post2);
            Assert.IsTrue(testedThread.getPosts().Count == count + 2);
            Assert.IsTrue(testedThread.getPosts().Contains(post1));
            Assert.IsTrue(testedThread.getPosts().Contains(post2));
        }

        [TestMethod]
        public void GetPostTest()
        {
            Member publisher = new Member("Publisher", "1234", true);
            GForum.GetForum().register("Publisher", "1234");
            GPost post1 = new GPost("Subject1", "Content", publisher, testedThread);
            GPost post2 = new GPost("Subject2", "Content", publisher, testedThread);
            int postID1 = testedThread.addPost(post1);
            int postID2 = testedThread.addPost(post2);
            Assert.IsTrue(testedThread.GetPost(postID1).Equals(post1));
            Assert.IsTrue(testedThread.GetPost(postID2).Equals(post2));
        }

        [TestMethod]
        public void EqualsTest()
        {
            GThread t1 = new GThread(1, "Subject", "Content", null);
            GThread t2 = new GThread(2, "Subject", "Content", null);
            Assert.IsFalse(t1.Equals(t2));
            t2 = new GThread(1, t2);
            Assert.IsTrue(t1.Equals(t2));
        }
    }
}
